package com.peng.stack_queue.array;

public class Queue {
    int size;
    int pushi;
    int popi;
    int limit;
    int arr[];

    public Queue(int limit) {
        this.size = 0;
        this.pushi = 0;
        this.popi = 0;
        this.limit = limit;
        this.arr = new int[limit];

    }

    public void push(int i){
        if(size==limit){
            throw new RuntimeException("queue已满！！");
        }
        size++;
        arr[pushi] = i;
        pushi = nextPos(pushi);
    }



    public int pop(){
        if(size==0){
            throw new RuntimeException("queue已空！！");
        }
        size--;
        int res = arr[popi];
        popi = nextPos(popi);

        return res;
    }


    private int nextPos(int i) {
        return i<limit-1?i+1:0;
    }

    public static void main(String[] args) {
        Queue queue = new Queue(5);

        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.push(5);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }

}
